<!DOCTYPE html>
{% load static %}
<html lang="zh-hans">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}我的博客系统{% endblock %}</title>

    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">

    <!-- 自定义CSS -->
    <style>
        :root {
            --primary-color: #0d6efd;
            --hover-color: #0b5ed7;
        }

        body {
            background-color: #f8f9fa;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .main-content {
            flex: 1;
            padding-top: 80px;
        }

        .navbar {
            box-shadow: 0 2px 4px rgba(0,0,0,.1);
        }

        .message-badge {
            position: relative;
            top: -1px;
            right: -5px;
        }

        .unread-count {
            background-color: var(--hover-color);
            color: white;
            border-radius: 10px;
            padding: 2px 8px;
            font-size: 0.8rem;
        }
    </style>

    {% block head %}{% endblock %}
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top">
        <div class="container">
            <a class="navbar-brand" href="/">
                <i class="bi bi-chat-dots"></i> 博客系统
            </a>

            <button class="navbar-toggler" type="button"
                    data-bs-toggle="collapse"
                    data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="{% url 'blog_list' %}">
                            <i class="bi bi-house-door"></i> 首页
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{% url 'messaging:inbox' %}">
                            <i class="bi bi-envelope"></i> 消息中心
                            {% if unread_count > 0 %}
                            <span class="unread-count">{{ unread_count }}</span>
                            {% endif %}
                        </a>
                    </li>
                </ul>

                <!-- 用户菜单 -->
                <div class="dropdown">
                    <a class="btn btn-outline-light dropdown-toggle"
                       href="#" role="button"
                       data-bs-toggle="dropdown">
                        <i class="bi bi-person-circle"></i>
                        {{ request.user.username }}
                    </a>
                    <ul class="dropdown-menu dropdown-menu-end">
                        <li>
                            <a class="dropdown-item" href="{% url 'profile' %}">
                                <i class="bi bi-person"></i> 个人中心
                            </a>
                        </li>
                        <li>
                            <a class="dropdown-item" href="{% url 'logout' %}">
                                <i class="bi bi-box-arrow-right"></i> 退出登录
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </nav>

    <!-- 主要内容 -->
    <main class="main-content">
        <div class="container">
            {% block content %}{% endblock %}
        </div>
    </main>

    <!-- 页脚 -->
    <footer class="bg-dark text-white mt-4">
        <div class="container py-4">
            <div class="row">
                <div class="col-md-6">
                    <h5>关于我们</h5>
                    <p>一个专业的博客交流平台</p>
                </div>
                <div class="col-md-6 text-end">
                    <p>&copy; 2024 博客系统</p>
                </div>
            </div>
        </div>
    </footer>

    <!-- Bootstrap 5 JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

    <!-- 通用JS -->
    <script>
        // 自动滚动到消息容器底部
        function scrollToBottom() {
            const container = document.querySelector('.message-container');
            if (container) {
                container.scrollTop = container.scrollHeight;
            }
        }

        // 页面加载完成后执行
        document.addEventListener('DOMContentLoaded', function() {
            scrollToBottom();

            // 标记消息为已读
            if (window.location.pathname.includes('/conversation/')) {
                fetch('{% url "messaging:mark_as_read" %}', {
                    method: 'POST',
                    headers: {
                        'X-CSRFToken': '{{ csrf_token }}',
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        conversation_id: {{ conversation.id|default:0 }}
                    })
                });
            }
        });
    </script>

    {% block scripts %}{% endblock %}
</body>
</html>